<%-- 
    Document   : AdminCategory
    Created on : 7-mag-2010, 17.59.08
    Author     : giuseppebrusadin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.PreparedStatement" %>
<%@page import="java.sql.ResultSet" %>
<%@page import="altri.database" %>
<%@page import="java.util.ArrayList" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Amministrazione Categorie</title>
    </head>
    <body>
        <%
                 Connection conn = database.connessione();
                 PreparedStatement ps = conn.prepareStatement("SELECT id,Nome,Categoria FROM Categorie ORDER BY id, Categoria ", ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
                 PreparedStatement ps2 = conn.prepareStatement("SELECT DISTINCT(Secondo.id),Secondo.Nome,Secondo.Categoria FROM Categorie as Primo, Categorie as Secondo WHERE Secondo.Categoria=Primo.id AND Primo.Categoria=1 ORDER BY Secondo.id,Secondo.Categoria", ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
                 ResultSet rs1 = ps2.executeQuery();

                 ResultSet rs = ps.executeQuery();
                 
                 out.println("Riepilogo categorie:");

                 while (rs.next()){
                    if (rs.getInt("id")!=1){
                       
                    
                    
                        
          
        %>
        <form action="DeleteCategoria.do" method="POST" >
            <%=rs.getString("Nome") %>
            <input type="hidden" name="Id" value=<%=rs.getInt("id")%> />
            <input type="submit" value="elimina" />
        </form>

        <%       
               }
               
               
                }
             
         %>
        Inserisci una nuova categoria:
        <form action="InsertCategoria.do" method="POST" >
            Nome: <input type="text" name="Titolo" ><br>
            Sottocategoria di:
            <select name="sottocat">
                <option value="/">Nessuna Categoria</option>
                <%
                    rs.first();
                    while (rs.next()){
                       if (rs.getInt("Categoria")==1 && rs.getInt("id")!=1){
                            out.print("<option value='"+rs.getInt("id")+"'>"+rs.getString("Nome")+"</option>");
                            rs1.beforeFirst();
                            while (rs1.next()){
                                if (rs1.getInt("Categoria")==rs.getInt("id")){
                                     out.print("<option value='"+rs1.getInt("id")+"'>"+rs1.getString("Nome")+"</option>");
                                }
                            }
                       }
                    }

                %>
            </select>
            <input type="submit" value="inserisci" />
        </form>

                <%
                rs.close();
                rs1.close();
                conn.close();
                %>
    </body>
</html>
